﻿/********************************************************************
	日期:2022/03/31
	名称:DynamicWnd
	邮箱:yunnibbd@163.com
	作者:yunni
	描述:动态ui元素界面
*********************************************************************/

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class DynamicWnd : WindowRoot{
    public Animation tipsAnim;
    public Text txtTips;
    public Transform hpItemRoot;

    private bool isTipsShow = false;

    private Queue<string> tipsQue = new Queue<string>();

    private Dictionary<string, ItemEntityHP> itemDic = new Dictionary<string, ItemEntityHP>();

    protected override void InitWnd(){
        base.InitWnd();
        SetActive(txtTips, false);
    }

    public void AddTips(string tips){
        lock (tipsQue){
            tipsQue.Enqueue(tips);
        }
    }

    private void Update(){
        if (tipsQue.Count > 0 && !isTipsShow){
            string tips = tipsQue.Dequeue();
            isTipsShow = true;
            SetTips(tips);
        }
    }

    #region Tips
    
    /*
     * @brief 在弹窗上显示一条tips
     * @param tips 内容
     */
    private void SetTips(string tips){
        SetActive(txtTips);
        SetText(txtTips, tips);

        AnimationClip clip = tipsAnim.GetClip("TipsShowAni");
        tipsAnim.Play();
        //延时关闭
        StartCoroutine(AniPlayDone(clip.length, () => {
            SetActive(txtTips, false);
            isTipsShow = false;
        }));
    }

    private IEnumerator AniPlayDone(float sec, Action cb){
        yield return new WaitForSeconds(sec);
        if (cb != null){
            cb();
        }
    }
    #endregion

    public void AddHPItemInfo(string name, Transform trans, int hp){
        ItemEntityHP item = null;
        if (itemDic.TryGetValue(name, out item)){
            return;
        }
        else{
            GameObject go = resSvc.LoadPrefab(PathDefine.HPItemPrefab, true);
            go.transform.SetParent(hpItemRoot);
            go.transform.localPosition = new Vector3(-1000, 0, 0);
            ItemEntityHP itemEntityHp = go.GetComponent <ItemEntityHP>();
            itemEntityHp.InitItemInfo(trans, hp);
            itemDic.Add(name, itemEntityHp);
        }
    }
    
    public void DelHPItemInfo(string name){
        ItemEntityHP item = null;
        if (itemDic.TryGetValue(name, out item)){
            Destroy(item.gameObject);
            itemDic.Remove(name);
        }
    }

    public void SetDodge(string key){
        ItemEntityHP item = null;
        if (itemDic.TryGetValue(key, out item)){
            item.SetDodge();
        }
    }
    
    public void SetCritical(string key, int critical){
        ItemEntityHP item = null;
        if (itemDic.TryGetValue(key, out item)){
            item.SetCritical(critical);
        }
    }
    
    public void SetHurt(string key, int hurt){
        ItemEntityHP item = null;
        if (itemDic.TryGetValue(key, out item)){
            item.SetHurt(hurt);
        }
    }

    public void SetHpVal(string key, int oldVal, int newVal){
        ItemEntityHP item = null;
        if (itemDic.TryGetValue(key, out item)){
            item.SetHpVal(oldVal, newVal);
        }
    }
}